﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// zhoubian_detail 的摘要说明
    /// </summary>
    public class zhoubian_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            int _user_id = DTRequest.GetFormInt("txtUserID", 0);
            BLL.zhoubian bll = new BLL.zhoubian();
            Model.zhoubian model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"user_id\":" + model.user_id + ",");
            strTxt.Append("\"title\":\"" + model.title + "\",");
            strTxt.Append("\"remark\":\"" + model.remark.ToString() + "\",");
            strTxt.Append("\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\",");
            strTxt.Append("\"hits\":" + model.hits + ",");
            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                    strTxt.Append("\"lianxiren\":\"" + userModel.user_name + "\",");
                    strTxt.Append("\"avatar\":\"http://www.shicaiquanzi.com" + userModel.avatar + "\",");
                    strTxt.Append("\"mobile\":\"" + userModel.mobile + "\",");
                }
            }
            else
            {
                strTxt.Append("\"lianxiren\":\"" + model.lianxiren.ToString() + "\",");
                strTxt.Append("\"avatar\":\"../image/default_avatar.png\",");
                strTxt.Append("\"mobile\":\"" + model.mobile.ToString() + "\",");
            }

            string imglist = string.Empty;
            if (model.albums != null)
            {
                foreach (Model.common_albums modelt in model.albums)
                {
                    if (modelt.original_path.Contains("shicaiquanzi"))
                    {
                        imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>";
                    }
                    else
                    {
                        imglist += "<div class=\"pic\"><img src=\"http://www.shicaiquanzi.com" + modelt.original_path + "\"/></div>";
                    }
                }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append("\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            else
            {
                strTxt.Append("\"imglist\":\"\"");
            }
            //判断是否有传过来用户名、密码
            if (new BLL.collection().Exists(4, id, _user_id))
            {
                strTxt.Append(",\"is_collect\":1");
            }
            else
            {
                strTxt.Append(",\"is_collect\":0");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}